﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_Adv_Manage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            PopulateDropdownlist();
        }
    }

    public void PopulateDropdownlist()
    {
        drpWZ.PopulateFromEnum<Adv.SYplace>();
      

        drpAddWZ.PopulateFromEnum<Adv.SYplace>();
    }

    public void PopulateDetail()
    {
        lvDetail.DataSource = Adv.GetAllByConditon(DongDong.Common.Misc.ConvertToInt(drpWZ.SelectedValue), txtSearchKey.Text).ToList();
        lvDetail.DataBind();
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        Adv objAdv = new Adv();
        objAdv.AdvPlace = DongDong.Common.Misc.ConvertToInt(drpAddWZ.SelectedValue);
        objAdv.Link = (txtAddLink.Text ?? "#");
        objAdv.Title = txtAddTitle.Text;
        if (fulAddLogo.HasFile)
            objAdv.ImgSrc = DongDong.Common.Misc.UploadFile(fulAddLogo, "/Resource/Adv/", ".jpg.jpeg.png.gif");
        else
            objAdv.ImgSrc = "no_image.jpg";

        objAdv.Add();
        DongDong.Common.MessageBox.ShowAndRedirect(this, "添加成功", "SYManage.aspx");
        PopulateDetail();
    }

    protected void lvDetail_ItemCanceling(object sender, ListViewCancelEventArgs e)
    {
        lvDetail.EditIndex = -1;
        PopulateDetail();
    }
    protected void lvDetail_ItemDeleting(object sender, ListViewDeleteEventArgs e)
    {

        int ID = DongDong.Common.Misc.ConvertToInt(lvDetail.DataKeys[e.ItemIndex].Value);
        Adv.Delete(ID);

        PopulateDetail();
    }
    protected void lvDetail_ItemUpdating(object sender, ListViewUpdateEventArgs e)
    {
        int ID = DongDong.Common.Misc.ConvertToInt(lvDetail.DataKeys[e.ItemIndex].Value);
        Adv objType = Adv.Get(ID);
        var item = lvDetail.Items[e.ItemIndex];
        DropDownList drpEditWZ = (DropDownList)item.FindControl("drpEditWZ");
        FileUpload fupEditImg = (FileUpload)item.FindControl("fupEditImg");
        TextBox txtEdiLink = (TextBox)item.FindControl("txtEdiLink");
        TextBox txtEditTitle = (TextBox)item.FindControl("txtEditTitle");
        objType.AdvPlace = DongDong.Common.Misc.ConvertToInt(drpEditWZ.SelectedValue);
        objType.Link = txtEdiLink.Text;
        if (fupEditImg.HasFile)
            objType.ImgSrc = DongDong.Common.Misc.UploadFile(fupEditImg, "/Resource/Adv/", ".jpg.jpeg.png.gif");
        objType.Title = txtEditTitle.Text;
        objType.Update();

        lvDetail.EditIndex = -1;
        PopulateDetail();
    }
    protected void lvDetail_ItemEditing(object sender, ListViewEditEventArgs e)
    {

        lvDetail.EditIndex = e.NewEditIndex;
        PopulateDetail();

        int ID = DongDong.Common.Misc.ConvertToInt(lvDetail.DataKeys[e.NewEditIndex].Value);
        Adv objAdv = Adv.Get(ID);
        DropDownList drpEditWZ = (DropDownList)lvDetail.Items[e.NewEditIndex].FindControl("drpEditWZ");
        drpEditWZ.PopulateFromEnum<Adv.SYplace>();
        drpEditWZ.SelectedValue = objAdv.AdvPlace.ToString();
    }

    protected void btnSerach_Click(object sender, EventArgs e)
    {
        PopulateDetail();
    }

    protected void Pager_PreRender(object sender, EventArgs e)
    {
        if (lvDetail.Items.Count == 0)
            PopulateDetail();
    }
    protected void lvDetail_PagePropertiesChanged(object sender, EventArgs e)
    {
        PopulateDetail();
    }
}